//
//  MusicListViewController.m
//  Music_KRC_Analysis
//
//  Created by mac on 2019/5/3.
//  Copyright © 2019 Yang. All rights reserved.
//

#import "MusicListViewController.h"
#import "MusicListTableViewCell.h"

#import "ViewController.h"

@interface MusicListViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation MusicListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTableView];
    [self initDataSource];
}

-(void)initDataSource
{
    NSArray *songArr = [[NSBundle mainBundle] pathsForResourcesOfType:@"" inDirectory:@"Song"];
    NSLog(@"songArr = %@",songArr);
    for (NSString *object in songArr) {
        NSString *name = [[object stringByDeletingPathExtension] lastPathComponent];
        NSString *lyricPath = [[NSBundle mainBundle] pathForResource:name ofType:@"krc"];
        NSString *lyricName = [[lyricPath stringByDeletingPathExtension] lastPathComponent];
        NSLog(@"lyricPath = %@",lyricPath);
        
        MusicModel *model = [[MusicModel alloc] init];
        model.name = name;
        model.songPath = object;
        model.lyricPath = lyricPath;
        model.lyricName = lyricName;
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
    
}

-(void)initTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[MusicListTableViewCell class] forCellReuseIdentifier:@"MusicListTableViewCell"];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicListTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MusicModel *model = self.dataSource[indexPath.row];
    cell.model = model;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MusicModel *model = self.dataSource[indexPath.row];
    ViewController *vc = [ViewController sharedInstance];
    vc.songArr = self.dataSource;
    vc.navigationItem.title = model.name;
    [vc showInVC:self withIndex:indexPath.row];
//    [self.navigationController pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
